package com.tvunetworks.center.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.user.mapper.MenuBarMapper;
import com.tvunetworks.center.user.model.Feature;
import com.tvunetworks.center.user.model.Menu;
import com.tvunetworks.center.user.model.MenuBar;
import com.tvunetworks.center.user.model.param.AddMenuBarParam;
import com.tvunetworks.center.user.model.vo.MenuBarVo;
import com.tvunetworks.center.user.service.FeatureService;
import com.tvunetworks.center.user.service.MenuBarService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: tvucc-aggregator
 * @description: MenuBarServiceImpl
 * @author lebronchen
 * @create: 2019-06-10 15:59
 **/
@Service
@Slf4j
public class MenuBarServiceImpl extends BaseService<MenuBar> implements MenuBarService {

    @Autowired
    private MenuBarMapper menuBarMapper;
    @Autowired
    private FeatureService featureService;


    @Override
    public void initMenuBar() {
        List<Menu> menus = menuBarMapper.listMenu();
        if (menus == null || menus.size() == 0) {
            return;
        }
        for (Menu menu : menus) {
            MenuBar menuBar = new MenuBar().setEnName(menu.getEnName())
                    .setCnName(menu.getChnName())
                    .setDataMark(menu.getDataMark())
                    .setOrderNo(menu.getOrderNo());
            super.insert(menuBar);
        }
    }

    @Override
    @Transactional
    public int addMenuBar(AddMenuBarParam param) {
        Feature feature = featureService.getById(param.getFeatureId());
        // feature 不存在
        if (feature == null) {
            return 2;
        }
        if (StringUtils.isBlank(param.getMenu())) {
            param.setMenu(feature.getFeature());
        }
        if (StringUtils.isBlank(param.getCnName())) {
            param.setCnName(feature.getCnName());
        }
        if (StringUtils.isBlank(param.getEnName())) {
            param.setEnName(feature.getEnName());
        }

        MenuBar menu = this.getByMenu(param.getMenu());
        if (menu != null) {
            // MenuBar已经存在
            return 1;
        }

        MenuBar menuBar = new MenuBar();
        BeanUtils.copyProperties(param, menuBar);
        if (menuBar.getOrderNo() == null) {
            // 如果没有设置 orderNo，那么设置一个最大的orderNo
            int maxOrderNo = menuBarMapper.getMaxOrderNo();
            menuBar.setOrderNo(maxOrderNo + 10);
        }
        super.insert(menuBar);
        return 0;
    }

    @Override
    @Transactional
    public int updateMenuBar(String id, AddMenuBarParam param) {
        MenuBar exist = this.getById(id);
        if (exist == null) {
            // MenuBar不存在
            return 1;
        }
        if (StringUtils.isNotBlank(param.getFeatureId())) {
            Feature feature = featureService.getById(param.getFeatureId());
            // feature 不存在
            if (feature == null) {
                return 2;
            }
        }
        MenuBar menuBar = this.getByMenu(param.getMenu());
        if (menuBar != null && !StringUtils.equals(id, menuBar.getId())) {
            // menu名已存在
            return 3;
        }
        BeanUtils.copyProperties(param, exist);
        super.update(exist);
        return 0;
    }

    @Override
    @Transactional
    public int deleteMenuBar(String id) {
        return super.deleteLogicById(id);
    }

    @Override
    public PageInfo<MenuBarVo> pageMenuBar(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MenuBarVo> list = menuBarMapper.listMenuBar();
        return new PageInfo<>(list);
    }

    @Override
    public List<MenuBar> listByFeatureId(String featureId) {
        if (StringUtils.isBlank(featureId)) {
            return null;
        }
        MenuBar record = new MenuBar().setFeatureId(featureId);
        return super.selectExistList(record);
    }

    @Override
    public List<MenuBarVo> listMenuBar() {
        return menuBarMapper.listMenuBar();
    }

    @Override
    public MenuBar getByMenu(String menu) {
        if (StringUtils.isBlank(menu)) {
            return null;
        }
        MenuBar menuBar = new MenuBar().setMenu(menu);
        return super.selectExistOne(menuBar);
    }

    @Override
    public MenuBar getById(String id) {
        return super.selectExistByPrimaryKey(id);
    }

}
